package Q392;

import java.util.Arrays;

/**
 * 判断子序列
 * @author 23737
 * @time 2021.11.27
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(new Solution().isSubsequence("axc", "ahbgdc"));
    }
}

/**
 * api
 */
class Solution {
    public boolean isSubsequence(String s, String t) {
        char[] chars = s.toCharArray();
        char[] chars2 = t.toCharArray();
        Arrays.sort(chars);
        Arrays.sort(chars2);
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        for (char aChar : chars) {
            stringBuilder.append(aChar);
        }
        for (char c : chars2) {
            stringBuilder2.append(c);
        }
        String s1 = stringBuilder.toString();
        String s2 = stringBuilder2.toString();
        return s2.contains(s1);
    }

    /**
     * 大佬解法
     * @param s
     * @param t
     * @return
     */
    public boolean isSubsequence2(String s, String t) {
        int index = -1;
        for (char c : s.toCharArray()){
            index = t.indexOf(c, index+1);
            if (index == -1) return false;
        }
        return true;
    }
}